using System;
using System.Collections;
using System.Collections.Generic;
using UI;
using UnityEngine;
using UnityEngine.UI;

public class LockNotebook : MonoBehaviour
{
    public int[] m_password;
    
    public EItemType m_consumeItemType;
    public string m_successTriggerScenario;

    private Text m_inputText;
    
    private int m_inputCount;
    private int[] m_input;
    private void Awake()
    {
        m_input = new int[m_password.Length];

        m_inputText = transform.Find("InputText").GetComponent<Text>();
        
        transform.Find("ConfirmBtn").GetComponent<Button>().onClick.AddListener(() =>
        {
            if (m_inputCount != m_password.Length)
            {
                m_inputCount = 0;
                m_inputText.text = null;
            
                UI_TipPanel.ShowTip("密码错误");
                return;
            }
            
            m_inputCount = 0;
            m_inputText.text = null;
            
            for (int i = 0; i < m_input.Length; i++)
            {
                if (m_input[i] != m_password[i])
                {
                    UI_TipPanel.ShowTip("密码错误");
                    return;
                }
            }

            UI_LittleGamePanel.Pass();
            if (m_consumeItemType != EItemType.None) InventoryManager.UseItem(m_consumeItemType);
            UI_ScenarioPanel.StartScenario(m_successTriggerScenario);
        });

        var btnRoot = transform.Find("Btns");
        for (int i = 0; i < btnRoot.childCount; i++)
        {
            var fig = i + 1;
            btnRoot.GetChild(i).GetComponent<Button>().onClick.AddListener(() =>
            {
                if (m_inputCount >= m_password.Length) return;
                m_input[m_inputCount] = fig;
                m_inputCount++;
                //更新文本
                m_inputText.text = "";
                for (int j = 0; j < m_password.Length; j++)
                {
                    if (j >= m_inputCount)
                    {
                        m_inputText.text += "_ ";
                    }
                    else
                    {
                        var cur = m_input[j];
                        m_inputText.text += $"{cur} ";
                    }
                }
                m_inputText.text = m_inputText.text.Trim();
            });
        }
    }
}
